package com.relytosoft.mizar.media.analyze.services.controller.receive;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.redis.utils.RedisUtil;
import com.relytosoft.mizar.media.common.constant.AyCacheConstants;
import com.relytosoft.mizar.media.common.constant.MediaMqConstants;
import com.relytosoft.mizar.media.common.emqx.client.MqttOp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * ReceiveController
 *
 * @Author SunLi
 * @Date: 2022/5/31 15:17
 * @Version 1.0
 */
@RestController
@RequestMapping("analyze")
public class ReceiveController {

    @PostMapping("/facestatus")
    public ResponseResult receiveFaceStatus(@RequestBody String body) {
        //接收数据
        JSONObject jsonObject = JSON.parseObject(body);
        Map<String, Object> info = new HashMap<>(3);
        info.put("taskId", jsonObject.getString("taskId"));
        info.put("code", jsonObject.getString("code"));
        info.put("msg", jsonObject.getString("msg"));
        //发送消息
        JSONObject mqttMsg = new JSONObject();
        mqttMsg.put("OpType", "updateTaskStatus");
        mqttMsg.put("Data", info);
        MqttOp.publish(MediaMqConstants.FACE_DATA_TOPIC, JSON.toJSONString(mqttMsg));

        return ResponseResult.ok();
    }

    @PostMapping("/strustatus")
    public ResponseResult receiveStruStatus(@RequestBody String body) {
        //接收数据
        JSONObject jsonObject = JSON.parseObject(body);
        Map<String, Object> info = new HashMap<>(3);
        info.put("taskId", jsonObject.getString("taskId"));
        info.put("code", jsonObject.getString("code"));
        info.put("msg", jsonObject.getString("msg"));
        //发送消息
        JSONObject mqttMsg = new JSONObject();
        mqttMsg.put("OpType", "updateTaskStatus");
        mqttMsg.put("Data", info);
        MqttOp.publish(MediaMqConstants.STRUCTURE_DATA_TOPIC, JSON.toJSONString(mqttMsg));

        return ResponseResult.ok();
    }

    @PostMapping("/strupictaskdata")
    public ResponseResult receiveStruPickTaskData(@RequestBody String body) {
        System.out.println(body);
        //把body转为json数组
        JSONArray jsonArray = JSON.parseArray(body);
        if (jsonArray.size() > 0) {
            //解析数组
            jsonArray.forEach(json -> {
                //json转为jsonObject
                JSONObject jsonObject = (JSONObject) json;
                RedisUtil.sSet(AyCacheConstants.AY_STRUCTURE_PIC_BY_PIC_RECEIVE + jsonObject.getString("ComparisonTaskID"),
                        jsonObject.toJSONString());
            });
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

}
